<template>
  <view v-if="cart.length !== 0">
    <!-- 收获地址 -->
    <my-address></my-address>

    <!-- 购物车 icon 标题 -->
    <view class="cart-title">
      <uni-icons type="shop" size="18"></uni-icons>
      <text class="cart-title_text">购物车</text>
    </view>

    <!-- 购物车商品列表 -->
    <uni-swipe-action>
      <block v-for="(goodsItem, index) in cart" :key="index">
        <uni-swipe-action-item :options="options" @click="swiperDeleteClick(goodsItem)">
          <my-goods :goods-item="goodsItem" :show-radio="true" :show-num="true" @radioHandle="radioHandle"
            @numChange="numChangeHandle"></my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>

    <!-- 结算区域 -->
    <my-settle></my-settle>
  </view>

  <!-- 空白购物车区域 -->
  <view v-else class="empty-container">
    <image class="empty-img" src="../../static/cart_empty@2x.png"></image>
    <text class="empty-text">空空如也 ~ </text>
  </view>
</template>

<script>
  // 导入封装的 mixin 模块
  import cartBadge from '@/mixins/tabbar-badge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    // 将 badgeMix 混入到当前的页面中进行使用
    mixins: [cartBadge],
    data() {
      return {
        options: [{
          text: '删除', // 显示的文本内容
          style: {
            backgroundColor: '#C00000' // 按钮的背景颜色
          }
        }]
      };
    },

    computed: {
      ...mapState('m_cart', ['cart'])
    },

    methods: {
      ...mapMutations('m_cart', ['updateCartRadio', 'updateGoodsCount', 'removeGoodsById']),

      // 商品的勾选状态发生了变化
      radioHandle(e) {
        // console.log(e)
        this.updateCartRadio(e)
      },


      // 商品的数量发生了变化
      numChangeHandle(val) {
        // console.log(val)
        this.updateGoodsCount(val)
      },

      // 滑动删除商品
      swiperDeleteClick(e) {
        // console.log(e)
        this.removeGoodsById(e.goods_id)
      }
    }
  }
</script>

<style lang="scss">
  .cart-title {
    display: flex;
    height: 40px;
    align-items: center;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;

    &_text {
      font-size: 14px;
      margin-left: 10px;
    }
  }

  .empty-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 150px;

    .empty-img {
      width: 90px;
      height: 90px;
    }

    .empty-text {
      font-size: 14px;
      margin-top: 15px;
      color: gray;
    }
  }
</style>
